﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.Mail
{
    /// <summary>
    /// SelectReceiver 的摘要说明。
    /// </summary>
    public partial class SelectReceiverPage : Page
    {
        protected int ClassID;
        protected int DispType;

        protected override void OnInit(EventArgs e)
        {
            listDept.SelectedIndexChanged += DeptListChange;

            ClassID = QSUtil.GetPositiveInt32("ClassID", 0);
            DispType = QSUtil.GetPositiveInt32("type", 0);
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PopulateData();
            }

            base.OnLoad(e);
        }

        #region 初始化下拉列表框
        /// <summary>
        /// 对数据进行初始化
        /// </summary>
        private void PopulateData()
        {
            // Read all accounts
            IList<StaffInfo> list;
            if (DispType == 1)
            {
                list = Staff.GetStaffInTeam(ClassID);
            }
            else
            {
                list = Staff.GetAllStaff(false);
            }
            listAccount.Items.Clear();
            foreach (var staff in list)
            {
                var listItem = new ListItem(staff.RealName, staff.Name);
                listAccount.Items.Add(listItem);
            }

            listDept.Items.Clear();
            foreach (var p in UDS.Components.Position.GetAllPositions())
            {
                listDept.Items.Add(new ListItem(p.Name, p.ID.ToString()));
            }
            listDept.Items.Insert(0, new ListItem("公司所有部门", "0"));
            listDept.SelectedIndex = 0;
        }
        #endregion

        #region 下拉列表事件
        public void DeptListChange(object sender, System.EventArgs e)
        {
            IList<StaffInfo> list;
            if (listDept.SelectedItem.Value == "0")
            {
                list = Staff.GetStaffInTeam(ClassID);
            }
            else
            {
                list = Staff.GetStaffByPosition(Int32.Parse(listDept.SelectedItem.Value));
            }

            listAccount.Items.Clear();
            foreach (var staff in list)
            {
                var listItem = new ListItem(staff.RealName, staff.Name);
                listAccount.Items.Add(listItem);
            }
        }
        #endregion
    }
}
